
var pageContext = new PageContext(settings||{});

pageContext.formInit = function(layero){
	
} 
pageContext.init = function(){//初始化
	pageContext.initEvent();
};
pageContext.toAdd = function(){
//	var index = layer.open({
//	  type: 2,
//	  content: window.global.ctx+'/sys/email/toedit?_json=0',
//	  area: ['620px', '595px'],
//	  maxmin: true
//	});
//	layer.full(index);
	showpage(window.global.ctx+'/sys/email/toedit?_json=0');
}
pageContext.save = function(){
	var form = $("#"+pageContext.setting.formId);
	$("#contentHide").text(pageContext.editor.getContent())
	 var para = form.formSerializeNotBlank(); 
	 if(form.validationEngine("validate")){
		 if(para.content){
			 var content = para.content;
			 if(content.length==0){
				 layer.alert("邮件内容不能为空！", {icon:0,title:"提示信息"});
				 return false;
			 }
		 }
		 var option = {
			 url:window.global.ctx+"/sys/email/save",
			 msg:"确定保存？",
			 failMsg:"保存失败", 
			 successMsg:"保存成功",
			 success:function(result){
				 location =  window.global.getFinalUrl(window.global.ctx+'/sys/email/index?_json=0'); 
			 }
		 };
		 pageContext.saveItem(para,option); 
	 }
}
pageContext.toEdit = function(id,option){
	 var para = {id:id};
	 pageContext.get(para,{
		 url:window.global.ctx+"/sys/email/toedit",
		 successMsg:"",
		 failMsg:"获取邮件失败",
		 success:function(result,para){
			 if(result.data){
				 //TODO 显示数据
				 var obj = result.data;
				 $("#categoryCodeId option[value='"+obj.categoryCode+"']").attr("selected","selected");  
				 $("#"+pageContext.setting.formId).find("input[name='subject']").attr("oldval",obj.subject).val(obj.subject);
				 $("#emailboxId option[value='"+obj.emailboxId+"']").attr("selected","selected");  
				 $('#emailboxId').trigger('chosen:updated');//更新选项
				 pageContext.editor.execCommand('insertHtml',obj.content);
			 }
		 }
	 });
}
jQuery(function($) {
	$('.toolbar .saveBtn').on("click", function (e) {
		 e.preventDefault();
		 pageContext.save();
		 return false;
	 }); 
   //loadEmailbox();
   pageContext.init({});
    $("#"+pageContext.setting.formId+" .inputElem").mailAutoComplete();
   $("#"+pageContext.setting.formId).validationEngine('attach', {promptPosition : "topLeft", autoPositionUpdate : true});
   var id = $("#id").val();
   if(id && id!=""){
	   //pageContext.toEdit(id); 
   }
   pageContext.editor = UM.getEditor('editor');
   pageContext.editor.ready(function() {
	    //设置编辑器的内容
	   pageContext.editor.setContent($("#contentHide").text());
	    //获取html内容，返回: <p>hello</p>
	    var html = pageContext.editor.getContent();
	    //获取纯文本内容，返回: hello
	    var txt = pageContext.editor.getContentTxt();
	});
//   pageContext.editor.execCommand('insertHtml',$("#contentHide").html());
   parent.iframeTab.iframeResize();
}); 
function loadData(id){
	
}
function configTag(flag){
  var tag_input = $(flag);
  try{
		tag_input.tag(
		  {
			placeholder:tag_input.attr('placeholder'),
			//enable typeahead by specifying the source array
			source: ace.vars['US_STATES'],//defined in ace.js >> ace.enable_search_ahead
			divclass:"tags tagsinput col-sm-5",
			inputclass:"inputElem"
			/**
			//or fetch data from database, fetch those that match "query"
			source: function(query, process) {
			  $.ajax({url: 'remote_source.php?q='+encodeURIComponent(query)})
			  .done(function(result_items){
				process(result_items);
			  });
			}
			*/
		  }
		)

		//programmatically add a new
//			var $tag_obj = $('.tagsinput').data('tag');
//			$tag_obj.add('Programmatically Added');
	}
	catch(e) {
		//display a textarea for old IE, because it doesn't support this plugin or another one I tried!
		if(tag_input.attr('id')){
			tag_input.after('<textarea id="'+tag_input.attr('id')+'" name="'+tag_input.attr('name')+'" rows="3">'+tag_input.val()+'</textarea>').remove();
		}else{
			tag_input.after('<textarea name="'+tag_input.attr('name')+'" rows="3">'+tag_input.val()+'</textarea>').remove();
		}
		//$('#form-fie1ld-tags').autosize({append: "\n"});
	} 
}
function loadEmailbox(){
	var url = window.global.ctx+"/sys/emailbox/query?noPage=1";
	var para = {validClass:1,deleteClass:0}
	 pageContext.get(para,{
		 url:url,
		 successMsg:"",
		 failMsg:"获取邮箱失败",
		 success:function(result,para){
			var array = [];
 			var data = result.data.list;
 			for ( var name in data ){ // 方法
					 var obj = data[name];
					 if(obj==null){
						continue;
					 }
					 array.push('<option value="'+obj.id+'">'+obj.name+'</option>')
				 }
 			$("#emailboxId").append(array.join(""));
 			if(!ace.vars['touch']) {
 				$('.chosen-select').chosen({allow_single_deselect:true,width:"200px"}); 
 				//resize the chosen on window resize
 		
 				$(window)
 				.off('resize.chosen')
 				.on('resize.chosen', function() {
 					$('.chosen-select').each(function() {
 						 var $this = $(this);
 						 $this.next().css({'width': $this.parent().width()});
 					})
 				}).trigger('resize.chosen');
 				//resize chosen on sidebar collapse/expand
 				$(document).on('settings.ace.chosen', function(e, event_name, event_val) {
 					if(event_name != 'sidebar_collapsed') return;
 					$('.chosen-select').each(function() {
 						 var $this = $(this);
 						 $this.next().css({'width': $this.parent().width()});
 					})
 				});
 			}
		 }
	 });
}